package com.yssystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yssystem.domain.SysPost;
import com.yssystem.domain.vo.R;
import com.yssystem.domain.vo.SysPostVO;
import com.yssystem.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysPost")
@CrossOrigin
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;
    //查询列表
    @GetMapping
    public List<SysPost> getPostList(){
    return sysPostService.list();
    }
    //分页加查询
    @GetMapping("/{current}/{size}")
    public R getPageAndSearch(@PathVariable("current") Integer current,@PathVariable("size") Integer size,SysPost sysPost){

        LambdaQueryWrapper<SysPost> lqw = new LambdaQueryWrapper<>();
        lqw.like(sysPost.getName() !=null && !sysPost.getName().isEmpty(),SysPost::getName,sysPost.getName());
        lqw.eq(sysPost.getPostCode()!=null && !sysPost.getPostCode().isEmpty(),SysPost::getPostCode,sysPost.getPostCode());
        lqw.eq(sysPost.getStatus()!=null,SysPost::getStatus,sysPost.getStatus());
        IPage<SysPost> page = new Page<>(current, size);
        IPage<SysPost> page1 = sysPostService.page(page, lqw);
        return new R(true,"查询成功",page1);
    }
    //添加
    @PostMapping
    public R addPost(@RequestBody SysPost sysPost){
        return new R(sysPostService.save(sysPost),"添加成功",sysPost,200);
    }
    //删除
    @DeleteMapping("/{id}")
    public R delPost(@PathVariable Integer id){
       return new R(sysPostService.removeById(id),"删除成功",200);
    }
    //修改
    @PutMapping
    public R putPost(@RequestBody SysPost sysPost){
        return new R(sysPostService.updateById(sysPost),"修改成功",sysPost,200);
    }



}
